package com.bungarus;

import com.bungarus.client.busi.ClientHandler;
import com.bungarus.codec.protobuf.MessageProtobuf;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by tang on 2018/11/11.
 */
public class AccessTestClient {
    public static void main(String[] args) throws Exception {
        new AccessTestClient().connect("localhost", 7700);
    }

    private void connect(String host, int port) throws Exception {
        Bootstrap client = new Bootstrap();
        client.group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        socketChannel.pipeline().addLast(new ProtobufDecoder(MessageProtobuf.Message.getDefaultInstance()));
                        socketChannel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        socketChannel.pipeline().addLast(new ProtobufEncoder());
                        socketChannel.pipeline().addLast(new ClientHandler());
                    }
                });
        Channel channel = client.connect("localhost", 9080).sync().channel();
        while (true){
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line = reader.readLine();

            MessageProtobuf.Header header = MessageProtobuf.Header.newBuilder()
                    .setId("1000")
                    .setFrom("from")
                    .setTo("to")
                    .setLang("lang")
                    .setGaction(0)
                    .setSessionId("")
                    .setPriority(0)
                    .setType(1)
                    .setCredential(MessageProtobuf.Credential
                            .newBuilder()
                            .setUsername("username")
                            .setPwd("password1")
                            .build())
                    .addExtensions(MessageProtobuf.Extension
                            .newBuilder()
                            .setKey("key")
                            .setValue("value")
                            .build())
                    .build();

            MessageProtobuf.Body body = MessageProtobuf.Body.newBuilder()
                    .setContent("ctnt:java.lang.String:good morning! Content from cmd line -> " + line)
                    .build();
            MessageProtobuf.Message message = MessageProtobuf.Message.newBuilder()
                    .setBody(body)
                    .setHeader(header)
                    .build();

            channel.writeAndFlush(message).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if(channelFuture.isSuccess()){
                        System.out.println("---------------------- client sent successfully!");
                    }
                }
            });
        }
    }
}
